define(function(require, exports, module) {
	require('jquery');
	require('fancybox');
	require('jscrollpane');
	require('mousewheel');
	require('jcookie');
	var user = require('user');

	function resumeModel(){
		this.init();
	};
	resumeModel.prototype = {
		skin: {
			userInfo: { tit: '个人资料', dis: '请完善您的个人信息，<br />他们会自动同步到您的简历上。', tips: '',submit: '确定', more: false},
			education: { tit: '教育信息', dis: '', tips: '<p>这一板块HR给你的时间只有10秒…20秒不能更多！</p><p>所以少年少女们，简单写明学校和主修专业。校园活动挑高大上的写，活动名称、担任职务、活动效果这三样就够了。</p><p>像多次获得廉师傅绿茶再来一瓶奖励这种就真不用写了。</p>',submit: '确定',more: true},
			jobrecord: { tit: '工作经验', dis: '', tips: '<p>你来到了HR一定会重点关注的板块。表紧张，按照下面的三段式来写：</p><p>公司名称<br />担任岗位<br />主要职责<br />三段句式:“负责了…”“参与了…”“获得了…”</p><p>就是这样So easy. 妈妈再也不用担心你写简历。</p><p>举例：<br />2010.4-2010.11<br />赫斯特广告有限公司上海分公司</p><p>订阅部实习生</p><p>主要负责时尚杂志天猫店首页网站的美化和修改工作，包括轮播图 、banner图等。另外还负责制作宣传展板，宣传DM单以及易拉宝等宣传制品，得到领导和同事的一致好评</p>',submit: '确定',more: true},
			mbti: { tit: '个人评价', dis: '', tips: '<p>想想你是怎么去追男神女神的，投其所好才是王道！</p><p>个人评价也是一样，针对不同岗位的要求凸显自己不同的特质，但是千万不要编造噢，因为HR的火眼金睛可是会马上识破的噢。</p><p>像市场、销售岗位更偏好性格外向的人；IT技术岗位更偏向逻辑清晰，动手能力强的人；银行、金融岗位更偏好对数字敏感的人……</p><p>如果还不知道该怎么写，你知道这世界上有一种叫MBTI职业测试的东西吗？</p><p>去做个测试，你的职场性格立马呈现在眼前。</p>',submit: '确定',testdis:'倘若您不知道该如何撰写自我评价，也可以利用我们的职<br />业测评工具帮助发现您的个人特质，从而给HR提供参考。', more: false},
			tags: { tit: '个性标签', dis: '', tips: '<p>恭喜你，终于来到了这个有趣又好玩的板块。这里有海量丰富的ICON供你选择。</p><p>点击最能代表你自己的标签，立刻展现在简历上。马上让你的简历创意十足，立刻和那些山炮土肥圆简历拉开差距！</p>',submit: '确定', more: false}
		},
		layout: {
			userInfo: [{type:'text',label:'中文名字',def:'',rule:$.testChinaName},{type:'text',label:'英文名字',def:'',rule:$.testEnglish},{type:'radio',label:'性别',radiolabel:{'male':'男','female':'女'},def:'male',rule:$.testBasic},{type:'text',label:'户籍',def:'',rule:$.testChinese},{type:'text',label:'出生日期',def:'1970/01/01',rule:$.testDate},{type:'text',label:'手机号码',def:'',rule:$.testMobile},{type:'text',label:'邮箱地址',def:'',rule:$.testMail}],
			education: [{type:'text',label:'毕业院校',def:'',rule:$.testBasic},{type:'text',label:'学历/学位',def:'',rule:$.testBasic},{type:'text',label:'系/专业',def:'',rule:$.testBasic},{type:'text',label:'毕业日期',def:'2015/01/01',rule:$.testDate},{type:'textarea',label:'专业介绍',def:'',sw:'size1of1',rule:$.testBasic}],
			jobrecord: [{type:'text',label:'公司名称',def:'',rule:$.testBasic},{type:'text',label:'部门/职位',def:'',rule:$.testBasic},{type:'text',label:'入职时间',def:'2015/01/01',rule:$.testDate},{type:'text',label:'离职时间',def:'2015/01/01',rule:$.testDate},{type:'textarea',label:'工作职责',def:'',sw:'size1of1',rule:$.testBasic}],
			mbti: [{type:'textarea',label:'',def:'',rule:$.testBasic,sw:'size1of0'}],
			tags: [{type:'tag',sw:'size1of0',arr:[{name:'兴趣',tags:['运动','看书','旅游','唱歌','音乐','街拍','写作','资深宅','小清新','数码控','自拍狂魔','女汉子','真直男','萌妹子','天然呆','二次元','八卦','吃货','发呆','极客','游戏达人']},{name:'设计',tags:['PS','Ai','Id','Fl','Ae','Dw','Pr','Lr','AutoCAD','3DMax','Cinema4D','Maya']},{name:'技术',tags:['Html','Java','JavaScript','C#','C++','MySQL','.Net','.Php','Node.js','Ruby','Oracle','VB','Python']}]}]
		},
		json: {
			userInfo: [],
			education: [],
			jobrecord: [],
			mbti: [],
			tags: []
		},
		creatHTML: function(_str,_nextStr){
			var sf = this,
				creatLiIndex = 0,
				str = _str ? _str : 'userInfo',
				nextStr = _nextStr ? _nextStr : '',
				skin = this['skin'][str],
				layout  = this['layout'][str],
				data = this['json'][str];
			var html = $('<div class="basic_info_wrap"><div class="basicForm '+str+'Form"><div class="basicFormInner '+str+'FormInner"><div class="form_wrap"><form name="form_'+str+'" id="form_'+str+'"><div class="form_list_scroll"><div class="form_list_scroll_inner"></div></div><div class="submit_bar"><input class="form_submit" name="'+str+'Submit" type="button" value="'+skin.submit+'" /></div></form></div></div></div></div>');
			var tit = $('<h2 class="tit txtC"><span class="txt">'+skin.tit+'</span></h2>').insertBefore(html.find('.form_wrap'));
			if(skin.dis){
				$('<div class="dis txtC">'+skin.dis+'</div>').insertAfter(tit);
			};
			if(skin.tips){
				var tip_pop = $('<div class="tip_pop"><h2>小贴士</h2><div class="context">'+skin.tips+'</div></div>').insertBefore(tit);
				var tip_toggle = $('<div class="inline_block tip_toggle"><span class="icon icon_tips"></span><span class="icon icon_tips_flw"></span></div>').appendTo(tit);
				$(tip_toggle)[0].bl = false;
				var fancy_block = $(html).find('.basicForm'),
					w = fancy_block.width(),
					ww = tip_pop.width();
				tip_toggle.on('click',function(){
					if($(this)[0].bl){
						$(this)[0].bl =  false;
						$(this).removeClass('active');
						fancy_block.width(w);
						tip_pop.hide();
						$.fancybox.toggle(1000);
					}else{
						$(this)[0].bl =  true;
						$(this).addClass('active');
						fancy_block.width(w+ww);
						tip_pop.show();
						$.fancybox.toggle(1000);
					}
				});
			};
			var form_list_scroll_inner = html.find('.form_list_scroll_inner');
			if(data.length){
				for(var ind=0;ind<data.length;ind++){
					creatLi(data[ind]);
				};
			}else{
				creatLi();
			};
			function creatLi(_data){
				creatLiIndex++;
				var list_wrap = $('<div class="form_list_wrap"></div>');
				var list = $('<ul class="form_list cf"></ul>');
				var tit = $('<div class="form_tit cf"><h3 class="left">'+skin.tit+creatLiIndex+'</h3></div>');
				if(skin.more){
					tit.appendTo(list_wrap);
					var del_btn = $('<span class="right icon_remove btn_remove">删除</span>').appendTo(tit);
					del_btn.on('click',function(){
						list_wrap.remove();
						html.find('.form_list_scroll').jScrollPane();
					});
				};
				for(var i=0;i<layout.length;i++){
					var form_in;
					var dot = layout[i];
					var val = _data && _data[i] ? _data[i] : '';
					if(dot['type']=='tag'){
						var arr = dot['arr'];
						var form_in = $('<div class="tag_form_in"></div>');
						var tag_nav = $('<div class="tag_nav cf"></div>');
						var tag_cont = $('<div class="tag_cont cf"></div>');
						var collect_culm = $('<div class="collect_culm cf"><div class="dis">您最多选择9个您的个性标签</div><div class="culm cf"></div></div>');
						for(var ii=0;ii<arr.length;ii++){
							var d = arr[ii];
							var dr = arr[ii]['tags'];
							var tag_nav_item = $('<div class="item tag_nav_item">'+d['name']+'</div>');
							var tag_cont_item = $('<div class="item tag_cont_item"></div>');
							tag_nav_item.appendTo(tag_nav);
							tag_cont_item.appendTo(tag_cont);
							for(var oo=0;oo<dr.length;oo++){
								var icon = $('<div class="dot dot_'+oo+'">'+dr[oo]+'</div>').appendTo(tag_cont_item);
							};
							if(ii==0){
								tag_nav_item.addClass('active');
								tag_cont_item.addClass('active');
							};
						};
						tag_nav.appendTo(form_in);
						tag_cont.appendTo(form_in);
						collect_culm.appendTo(form_in);
						tag_nav.find('.item').on('click',function(){
							$(this).addClass('active').siblings().removeClass('active');
							tag_cont.find('.item').eq($(this).index()).addClass('active').siblings().removeClass('active');
						});
					};
					if(dot['type'] == 'text'){
						form_in = $('<input type="text" id="'+str+'_'+creatLiIndex+'_'+i+'" name="'+str+'_'+creatLiIndex+'_'+i+'" value="'+val+'" class="form_culm form_text" placeholder="'+dot['def']+'" />');
					};
					if(dot['type'] == 'radio'){
						var rl = dot['radiolabel'];
						form_in = $('<div></div>');
						for(var o in rl){
							var radio_item = $('<div class="left radio_item"><label class="cos_radio" for="'+(str+'_'+creatLiIndex+'_'+i+o)+'"><div class="cos_inner"><input id="'+(str+'_'+creatLiIndex+'_'+i+o)+'" name="'+(str+i)+'" value="'+o+'" type="radio" class="form_radio" /></div></label><span class="flw left">'+rl[o]+'</span></div>');
							var n = val ? val : dot['def'];
							if(n == o){
								radio_item.addClass('checked');
								radio_item.find('input').attr('checked', true);
							}else{
								radio_item.find('input').attr('checked', false);
							}
							radio_item.appendTo(form_in);
						};
						form_in.find('.radio_item').each(function(index,item){
							$(item).find('input').on('change',function(e){
								if(e.currentTarget.checked){
									$(item).addClass('checked').siblings().removeClass('checked');
								};
							})
						});
					};
					if(dot['type'] == 'textarea'){
						form_in = $('<textarea id="'+str+'_'+creatLiIndex+'_'+i+'" name="'+str+'_'+creatLiIndex+'_'+i+'" class="form_culm form_textarea">'+val+'</textarea>');
					};
					var sw = dot['sw'] ? dot['sw'] : 'size1of2';
					console.log(dot['sw'])
					var li = $('<li data-index="'+i+'" class="'+sw+'"><div class="input_text_wrap left"></div></li>');
					if(dot['label']){
						$('<label class="left">'+dot['label']+'</label>').prependTo(li);
					}
					form_in.appendTo(li.find('.input_text_wrap'));
					li.appendTo(list);
				};
				list.appendTo(list_wrap);
				list_wrap.appendTo(form_list_scroll_inner);
			};
			if(skin.more){
				var add_bar = $('<div class="add_bar cf"><span class="right icon_add add_btn">添加'+skin.tit+'</span></div>').insertAfter(html.find('.form_list_scroll'));
				add_bar.find('.add_btn').on('click',function(){
					if(creatLiIndex>=3){
						return false;
					};
					creatLi();
					if(creatLiIndex>=3){
						add_bar.addClass('disable');
					}
					html.find('.form_list_scroll').jScrollPane();
				});
				setTimeout(function(){
					html.find('.form_list_scroll').jScrollPane();
				},100);
			};
			if(skin.testdis){
				$('<div class="bar_test_mbti cf"><span class="txt">'+skin.testdis+'</span><div class="btn_test_mbti"></div></div>').appendTo(form_list_scroll_inner);
			}
			$(html).find('.form_submit').on('click',formFn);
			function formFn(e){
				var form = html.find('form'),
					postData = [],
					passBl = true;
				$(form).find('ul.form_list').each(function(){
					var ulPostData = [];
					$(form).find('li').each(function(index,item){
						var i = $(item).data('index');
						var rule = sf['layout'][str][i]['rule'];
						var val = '';
						if($(item).find('input:radio').length){
							val = $(item).find('input:checked').val();
						}else if($(item).find('textarea').length){
							val = $.trim($(item).find('textarea').val());
						}else{
							val = $.trim($(item).find('input').val());
						};
						ulPostData[i] = val;
						var tests = true;
						if(rule){
							tests = rule(val);
						};
						if(val===''){
							tests = false;
						};
						if(!tests){
							passBl = false;
							$(item).addClass('error');

						}else{
							$(item).removeClass('error');
						};
					});
					postData.push(ulPostData);
				});
				if(passBl){
					console.log('post',postData);
					sf['json'][str] = postData;
					if(nextStr){ // && !sf['json'][nextStr]
						sf['show_'+nextStr]();
					}else{
						$.fancybox.close();
					};
				}else{
					console.log('false',postData);
				}
			};
			return html;
		},
		show_tags: function(_obj){
			var sf= this;
			var str = 'tags';
			var nextStr = '';
			var html = this.creatHTML(str,nextStr);
			$.fancybox.open(html,{
				wrapCSS : 'own_skin form_skin',
				padding : 0,
				beforeLoad: function(){},
				afterLoad: function(){},
				afterShow: function(){},
				afterClose: function(){}
			});
		},
		show_mbti: function(_obj){
			var sf= this;
			var str = 'mbti';
			if(_obj && _obj.nextBl){
				var nextStr = '';
			}else{
				var nextStr = 'tags';
			};
			var html = this.creatHTML(str,nextStr);
			$.fancybox.open(html,{
				wrapCSS : 'own_skin form_skin',
				padding : 0,
				beforeLoad: function(){},
				afterLoad: function(){},
				afterShow: function(){},
				afterClose: function(){}
			});
		},
		show_jobrecord: function(_obj){
			var sf= this;
			var str = 'jobrecord';
			if(_obj && _obj.nextBl){
				var nextStr = '';
			}else{
				var nextStr = 'mbti';
			};
			var html = this.creatHTML(str,nextStr);
			$.fancybox.open(html,{
				wrapCSS : 'own_skin form_skin',
				padding : 0,
				beforeLoad: function(){},
				afterLoad: function(){},
				afterShow: function(){},
				afterClose: function(){}
			});
		},
		show_education: function(_obj){
			var sf= this;
			var str = 'education';
			if(_obj && _obj.nextBl){
				var nextStr = '';
			}else{
				var nextStr = 'jobrecord';
			};
			var html = this.creatHTML(str,nextStr);
			$.fancybox.open(html,{
				wrapCSS : 'own_skin form_skin',
				padding : 0,
				beforeLoad: function(){},
				afterLoad: function(){},
				afterShow: function(){},
				afterClose: function(){}
			});
		},
		show_userInfo: function(_obj){
			var sf= this;
			var str = 'userInfo';
			if(_obj && _obj.nextBl){
				var nextStr = '';
			}else{
				var nextStr = 'education';
			};
			var html = this.creatHTML(str,nextStr);
			$.fancybox.open(html,{
				wrapCSS : 'own_skin form_skin',
				padding : 0,
				beforeLoad: function(){},
				afterLoad: function(){},
				afterShow: function(){},
				afterClose: function(){}
			});
		},
		init: function(){
			
		}
	};
	$(document).ready(function(){
		var resume_model = new resumeModel();
		setTimeout(function(){ //cos ajax
			var ajaxBl = true;
			var msg = {
				userInfo : [['胡子','Holy','female','湖南','1983/11/11','13465575736','ad@ad.com']],
				education : [['冲突','本科','中文','2015/01/01','discription']],
				jobrecord : [['冲突','本科','2015/01/01','2015/01/01','discriptions']],
				mbti : [['冲突']]
			};
			for(var i in msg){
				resume_model['json'][i] = msg[i];
			};
			if(!ajaxBl){
				resume_model.show_userInfo({nextBl:false});
			};
		},200);
	});
});

